package com.snopy.configs;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * TODO
 *
 * @author snoopy
 * @date 2020/9/19 0019 19:52
 * @email 77912204@qq.com
 * @remark
 */
@Configuration
public class CorsConfig {
    public CorsConfig(){}

    @Bean
    public CorsFilter corsFilter(){
        //1.添加cors配置信息
        CorsConfiguration corsConfiguration  = new CorsConfiguration();
        //允许通过的url
        corsConfiguration.addAllowedOrigin("http://localhost:8080");
        //设置允许发送cookie信息
        corsConfiguration.setAllowCredentials(true);
        //设置允许的请求方式
        corsConfiguration.addAllowedMethod("*");
        //设置允许的header
        corsConfiguration.addAllowedHeader("*");
        //2.为url添加映射路径
        UrlBasedCorsConfigurationSource corsSource = new UrlBasedCorsConfigurationSource();
        corsSource.registerCorsConfiguration("/**",corsConfiguration);
        //3.返回定义好的corsSource

        return new CorsFilter(corsSource);
    }
}
